// pages/answer/answer.js
const app = getApp();
const Controller = require('../controllers/controllers.js').controller;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    selectedSex: 1,
    list: [
      {
        title: 'A. 没有（根本不）',
        answer: 1
      },
      {
        title: 'B. 很少（有一点）',
        answer: 2
      },
      {
        title: 'C. 有时（有些）',
        answer: 3
      },
      {
        title: 'D. 经常（相当）',
        answer: 4
      },
      {
        title: 'E. 总是（非常）',
        answer: 5
      }
    ],
    question: [],
    id: '',
    sex: 0,
    speed: 0,
    answer: [],
  },

  //选择性别
  selectSex(e) {
    let sex = e.currentTarget.dataset["sex"]
    sex = Number(sex)
    this.setData({
      selectedSex: sex
    })
  },

  questionList() {
    Controller.questionList({
      sex: this.data.sex
    }).then(res => {
      if (res.code == 0) {
        let question = res.data.baseData
        let answer = []
        question.forEach(item => {
          answer.push(0)
          // answer.push(1)
        })
        this.setData({
          question,
          answer
        })
      }
    })
  },

  selectAnswer(e){
    console.log(e.currentTarget.dataset)
    let answer = e.currentTarget.dataset["answer"]
    let answerList = this.data.answer
    answerList[this.data.speed] = Number(answer)
    this.setData({
      answer:answerList
    })
  },

  prev(){
    if(this.data.speed==0){
      wx.showToast({
        title: '已经是第一题',
        icon: 'none',
        duration: 1200
      });
      return
    }
    this.setData({
      speed:this.data.speed-1
    })
  },

  next(){
    if(this.data.answer[this.data.speed]==0){
      wx.showToast({
        title: '请先选择答案',
        icon: 'none',
        duration: 1200
      });
      return
    }
    if(this.data.speed==this.data.question.length-1){
      this.submit()
      return
    }
    // if(this.data.speed==9){
    //   this.submit()
    //   return
    // }
    this.setData({
      speed:this.data.speed+1
    })
  },

  submit(){
    if(!wx.getStorageSync('logintoken')){
      app.ToLogin()
      return
    }
    let answers = []
    this.data.question.forEach((item,index)=>{
      answers.push(String(item.type)+'-'+String(this.data.answer[index]))
    })
    answers = answers.join(',')
    let data = {
      answers
    }
    if(this.data.id!=''){
      data.familyMemberId = this.data.id
    }
    Controller.physiqueExam(data).then(res=>{
      if(res.code==0){
        let id = res.data.baseData.id
        wx.navigateTo({
          url:"/pages/assessmentResults/assessmentResults?id="+id
        });
      }
    })
  },

  //开始测试
  start() {
    if (this.data.selectedSex == 0) {
      wx.showToast({
        title: '请先选择性别',
        icon: 'none',
        duration: 1200
      });
      return
    }
    Controller.updateInfo({
      sex: this.data.selectedSex
    }).then(res => {
      if (res.code == 0) {
        this.setData({
          sex: this.data.selectedSex
        })
        this.questionList()
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.parentid){
      app.globalData.parentid=options.parentid
    }
    if (options.id) {
      this.setData({
        id: options.id
      })
    }
    if (options.sex) {
      this.setData({
        sex: options.sex,
        selectedSex: options.sex
      })
      if (this.data.sex != 0) {
        this.questionList()
      }
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  onShareAppMessage: function (e) {
    let userInfo=wx.getStorageSync('userInfo') || '';
    var parentid='';
    if(userInfo){
      parentid=userInfo.id
    }
    var path="pages/selectMember/selectMember?parentid="+parentid;

    let title = '';
    let imageUrl = '';
    let obj = {
      title,
      imageUrl,
      path,
      success: function (res) {},
      fail(err) {}
    }
    return obj
  },
  onShareTimeline: function(res){
    let userInfo=wx.getStorageSync('userInfo') || '';
    var parentid='';
    if(userInfo){
      parentid=userInfo.id
    }
    var path="pages/selectMember/selectMember?parentid="+parentid;

    let title = '';
    let imageUrl = '';
    let obj = {
      title,
      imageUrl,
      path,
      success: function (res) {},
      fail(err) {}
    }
    return obj
  },
})